﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace UI.Forms
{
    public partial class crm_contact_N : FormBase1
    {
        private int count = 0;
        string caname = "";
        public delegate void DoAction(string name);//如果有参数的话，定义参数 
        private BindingList<SVC.client.CRM_Client> clients;
        public DoAction FatherRefresh;
        private Guid? cid;
        public crm_contact_N(Guid? _clientid)
        {
            InitializeComponent();
            
            cid = _clientid;
        }

        private void btn_new_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            vaildbefore();
            if (dvp.Validate())
            {
                SVC.client.CRM_Contact ca = new SVC.client.CRM_Contact();
                ca.Name = te_name.Text;
                ca.ClientID = new Guid(lookUpEdit1.EditValue.ToString());
                ca.Department = textEdit2.Text.Trim();
                ca.Mobile = textEdit1.Text;
                ca.IsMain = false;
                string r = my.bw.CRM_Contact_Add(ca, my.ACID);
                if (string.IsNullOrEmpty(r))
                {
                    count++;
                    caname = te_name.Text;
                    reset();
                }
                else
                {
                    Comm.ShowErorr(r);
                }
            }
        }

        private void vaildbefore()
        {
            te_name.Text = te_name.Text.Trim();
        }
        void reset()
        {
            te_name.Text = "";
            textEdit1.Text = "";
            textEdit2.Text = "";
        }

        private void btn_nowclose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            vaildbefore();
            if (dvp.Validate())
            {
                SVC.client.CRM_Contact ca = new SVC.client.CRM_Contact();
                ca.Name = te_name.Text;
                ca.ClientID = new Guid(lookUpEdit1.EditValue.ToString());
                ca.Department = textEdit2.Text.Trim();
                ca.Mobile = textEdit1.Text;
                ca.IsMain = false;
                string r = my.bw.CRM_Contact_Add(ca, my.ACID);
                if (string.IsNullOrEmpty(r))
                {
                    count++;
                    caname = te_name.Text;
                    this.Close();
                }
                else
                {
                    Comm.ShowErorr(r);
                }
            }
        }

        private void Carrier_N_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (count > 0)
            {
                FatherRefresh(caname);
            }
        }

        private void crm_contact_N_Load(object sender, EventArgs e)
        {
            clients = my.bw.CRM_Client_Get("", my.ACID);
            lookUpEdit1.Properties.DataSource = clients;
            lookUpEdit1.Properties.DisplayMember = "ClientName";
            lookUpEdit1.Properties.ValueMember = "ClientID";
            
            if (cid!=null)
            {
                lookUpEdit1.Properties.ReadOnly = true;
                lookUpEdit1.EditValue = cid;
            }
        }
    }
}